Spring Security with Oauth2 Client

您所在的位置:网站首页 spring cloud oauth2 client Spring Security with Oauth2 Client

Spring Security with Oauth2 Client

#Spring Security with Oauth2 Client| 来源: 网络整理| 查看: 265

Oauth2 客户端

环境:Java11 + Springboot 2.1.3

节选自《Netkiller Spring Cloud 手札》

4.0.0 cn.netkiller parent 0.0.1-SNAPSHOT cn.netkiller oauth2-client 0.0.1-SNAPSHOT oauth2-client http://maven.apache.org UTF-8 org.springframework.boot spring-boot-starter-oauth2-client org.springframework.boot spring-boot-starter-web junit junit test

application.yml

server: port: 8080 APP-CLIENT-ID: 180579ba67875ffc18e3 APP-CLIENT-SECRET: 8175af8f5691e2f3b6007b9597d8c1e3499e15a0 spring: # redis: # host: localhost security: oauth2: client: provider: netkiller: authorization-uri: http://localhost:8080/oauth/authorize token-uri: http://localhost:8080/oauth/token user-info-uri: http://localhost:8080/user user-name-attribute: name # token-endpoint-url: http://localhost:8080/oauth/check_token yahoo-oidc: issuer-uri: https://api.login.yahoo.com registration: netkiller: client-id: sso client-secret: secret client-name: Neo provider: netkiller scope: read authorization-grant-type: authorization_code redirect-uri: http://localhost:${server.port}/login/oauth2/code/netkiller github-client-1: client-id: ${APP-CLIENT-ID} client-secret: ${APP-CLIENT-SECRET} client-name: Github user provider: github scope: user redirect-uri: http://localhost:${server.port}/login/oauth2/code/github github-client-2: client-id: ${APP-CLIENT-ID} client-secret: ${APP-CLIENT-SECRET} client-name: Github email provider: github scope: user:email redirect-uri: http://localhost:${server.port}/login/oauth2/code/github yahoo-oidc: client-id: ${YAHOO-CLIENT-ID} client-secret: ${YAHOO-CLIENT-SECRET} github-repos: client-id: ${APP-CLIENT-ID} client-secret: ${APP-CLIENT-SECRET} scope: public_repo redirect-uri: "{baseUrl}/github-repos" provider: github client-name: GitHub Repositories

logging:

level: # org.springframework.web: DEBUG org.springframework.security: DEBUG

SpringApplication

package cn.netkiller.oauth2.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class Application { public static void main(String[] args) { System.out.println("Oauth2 Client!"); SpringApplication.run(Application.class, args); } }

WebSecurityConfigurer

package cn.netkiller.oauth2.client; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated().and().oauth2Login(); } }

TestController

package cn.netkiller.oauth2.client; import java.security.Principal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private ClientRegistrationRepository clientRegistrationRepository; @RequestMapping("/") public Principal email(Principal principal) { System.out.println("Hello " + principal.getName()); return principal; } @RequestMapping("/index") public ClientRegistration index() { ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("netkiller"); return clientRegistration; } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3